package com.carefree.system.modules.admin.controller;

import com.carefree.common.core.PageUtils;
import com.carefree.common.core.Result;
import com.carefree.common.emus.BusinessTypeEnum;
import com.carefree.common.emus.OperatorTypeEnum;
import com.carefree.common.emus.StatusCodeEnum;
import com.carefree.system.common.annotation.SystemLog;
import com.carefree.system.common.utils.AliyunOssUtils;
import com.carefree.system.modules.admin.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 文件上传
 *
 * @author tengbin
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/sys/oss")
public class OssController {

    @Resource
    private OssService ossService;

    @Resource
    private AliyunOssUtils aliyunOssUtils;


    /**
     * 列表
     *
     * @param params 分页参数
     * @return
     */
    @SystemLog(value = "分页查询文件上传记录", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "分页查询文件上传记录")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('btn:oss:list')")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
        PageUtils page = ossService.queryPage(params);
        return Result.success(page);
    }


    /**
     * 服务端签名直传
     *
     * @return
     */
    @SystemLog(value = "服务端签名直传", businessType = BusinessTypeEnum.OTHER, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "服务端签名直传")
    @GetMapping("/policy")
    @PreAuthorize("hasAuthority('function:oss:policy')")
    public Result<Map<String, String>> policy() {
        Map<String, String> policy = aliyunOssUtils.policy();
        return Result.success(policy);
    }


    @ApiOperation(value = "保存服务端签名直传上传的图片")
    @PreAuthorize("hasAuthority('btn:oss:upload')")
    @PostMapping("/savePolicyImgs")
    public Result<Object> savePolicyImg(@RequestBody String[] urls) {
        // 数据库保存上传记录
        return ossService.saveDatas(urls) ? Result.ok() : Result.error(StatusCodeEnum.UPLOAD_IMG_EXCEPTION.getCode(), StatusCodeEnum.UPLOAD_IMG_EXCEPTION.getMessage());
    }


    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @SystemLog(value = "图片上传(预留接口)", businessType = BusinessTypeEnum.INSERT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "图片上传(预留接口)")
    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('btn:oss:upload')")
    public Result<String> upload(@RequestParam("file") MultipartFile file) {
        // 图片上传
        String fileUrl = aliyunOssUtils.uploadImages(file);
        // 数据库保存上传记录
        return ossService.saveData(fileUrl) ? Result.success(fileUrl) : Result.error(StatusCodeEnum.UPLOAD_IMG_EXCEPTION.getCode(), StatusCodeEnum.UPLOAD_IMG_EXCEPTION.getMessage());
    }


    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @SystemLog(value = "根据id批量删除文件上传记录", businessType = BusinessTypeEnum.DELETE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id批量删除文件上传记录")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('btn:oss:remove')")
    public Result<Object> delete(@RequestBody Long[] ids) {
        return ossService.deleteImagesByIds(Arrays.asList(ids)) ? Result.ok() : Result.error();
    }


}
